package com.cqs.leetcode.dp;

/**
 * lixw
 * 2021/11/6
 **/
public class BestBuySellStockCooldown309 {

    //按状态分析转移方程
    public int maxProfit(int[] prices) {
        int[][] dp = new int[prices.length][3];
        //购买为负的收益，卖出为正的收益
        dp[0][0] = -1 * prices[0]; //持有一直股票的max收益
        dp[0][1] = 0; //不持有股票且冷冻期的收益
        dp[0][2] = 0; //不持有股票 不在冷冻期的收益
        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
            dp[i][1] = dp[i - 1][2] + prices[i];
            dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1]);
        }
        return Math.max(dp[prices.length - 1][1], dp[prices.length - 1][2]);
    }
}
